﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DevExpress.Web.ASPxUploadControl;
using UdrinkWater_Project;

namespace Project_Udrinkwater.PagesAdmin
{
    public partial class Add_And_Edit_Goods : System.Web.UI.Page
    {
        AddProductDataContext db = new AddProductDataContext();
        // DataClassesProductDataContext dbp = new DataClassesProductDataContext();
        string Connection = ConfigurationManager.ConnectionStrings["DB_Udrink"].ConnectionString;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["Product_ID"]==null)
            {
                Pn_Insert.Visible = true;
            }
            else
            {
                Pn_Update.Visible = true;
                EditGoodsdata(Session["Product_ID"].ToString());
            }
        }
        public void EditGoodsdata(string GoodID)
        {
            //ดึงข้อมูลมาแก้ไข
            Pn_Update.Visible = true;
            Pn_Insert.Visible = false;
            Image1.Visible = true;
            var q = (from a in db.TB_Products
                     where a.Product_ID == GoodID
                     select a).First();
            tb_GoodsIDUpdate.Text = q.Product_ID;
            tb_GoodsNameUpdate.Text = q.Product_name;
            tb_PriceUpdate.Text = q.Price.ToString();
            //tb_LoanUpdate.Text = q.Price_Loan.ToString();
            Image1.ImageUrl = q.Image_Path;
            cb_UnitUpdate.SelectedIndex = Convert.ToInt32(q.Unit - 1);
            Session["UrlImage"] = q.Image_Path;
           
        }

        protected void ASPxButton1_Click(object sender, EventArgs e)
        {
            if (FileUpload1.PostedFile != null)
            {
                string FileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
                FileUpload1.SaveAs(Server.MapPath("~/Images_Goods/" + FileName));

               //เพิ่มสินค้าใหม่ที่ Table Product
                TB_Product TbPro = new TB_Product();
                TbPro.Product_ID = tb_GoodsIDInsert.Text;
                TbPro.Product_name = tb_GoodsName.Text;
                TbPro.Price = Convert.ToInt32(tb_Price.Text);
                TbPro.Unit = Convert.ToInt32(cb_UnitInsert.Value);
                TbPro.Image_Path = "~/Images_Goods/" + FileName;
                TbPro.In_Stock = 0;
                db.TB_Products.InsertOnSubmit(TbPro);
                db.SubmitChanges();

                tb_GoodsName.Text = "";
                tb_Price.Text = "";
               // tb_Loan.Text = "";
                tb_GoodsIDInsert.Text = "";
                //tb_PriceRetial.Text = "";
                Pn_Insert.Visible = false;

                Response.Redirect("GoodsList.aspx");
               // ASPxGridView1.DataBind();
            }
        }

        protected void bt_Cancle_Click(object sender, EventArgs e)
        {
            Response.Redirect("GoodsList.aspx");
        }

        protected void bt_CancleUpdate_Click(object sender, EventArgs e)
        {
            Response.Redirect("GoodsList.aspx");
        }

        protected void bt_Update_Click(object sender, EventArgs e)
        {
            var q = (from a in db.TB_Products
                     where a.Product_ID == tb_GoodsIDUpdate.Text
                     select a).First();
            //แก้ไขและบันทึกข้อมูลสินค้า และรูปภาพ
            if (FileUpload2.FileName != "")
            {
                string FileName = Path.GetFileName(FileUpload2.PostedFile.FileName);
                FileUpload2.SaveAs(Server.MapPath("~/Images_Goods/" + FileName));
                FileInfo FileIn = new FileInfo(Server.MapPath(Session["UrlImage"].ToString()));
                FileIn.Delete();
                q.Product_ID = tb_GoodsIDUpdate.Text;
                q.Product_name = tb_GoodsNameUpdate.Text;
                q.Price = Convert.ToInt32(tb_PriceUpdate.Text);
                //q.Price_Loan = Convert.ToInt32(tb_LoanUpdate.Text);
                q.Image_Path = "~/Images_Goods/" + FileName;
                q.Unit = Convert.ToInt32(cb_UnitUpdate.Value);
                db.SubmitChanges();
            }
            else
            {
                q.Product_ID = tb_GoodsIDUpdate.Text;
                q.Product_name = tb_GoodsNameUpdate.Text;
                q.Price = Convert.ToInt32(tb_PriceUpdate.Text);
                //q.Price_Loan = Convert.ToInt32(tb_LoanUpdate.Text);
                q.Unit = Convert.ToInt32(cb_UnitUpdate.Value);
                db.SubmitChanges();
            }
            
            tb_GoodsIDUpdate.Text = "";
            tb_GoodsNameUpdate.Text = "";
            tb_PriceUpdate.Text = "";
          //tb_LoanUpdate.Text = "";
          //tb_PriceUpdateDetial.Text = "";
            Image1.ImageUrl = "";
            Image1.Visible = false;
            Pn_Update.Visible = false;

            Response.Redirect("GoodsList.aspx");
           // ASPxGridView1.DataBind();
        }

       

    }
}